package chaos.main;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.jme.app.BaseSimpleGame;
import com.jme.input.KeyBindingManager;
import com.jme.input.MouseInput;
import com.jme.renderer.ColorRGBA;
import com.jmex.bui.BRootNode;
import com.jmex.bui.BStyleSheet;
import com.jmex.bui.BuiSystem;
import com.jmex.bui.PolledRootNode;

public abstract class GUIGCore implements GCore {
	protected PolledRootNode _root;
	
	public void init(CBFSystem gameState) {
		_root = new PolledRootNode(gameState.getTimer(), gameState.getInput());
		BuiSystem.init(_root, "/rsrc/style2.bss");
		gameState.getRootNode().attachChild(BuiSystem.getRootNode());
        
        // we don't hide the cursor
        MouseInput.get().setCursorVisible(true);
        
//        // load up the default BUI stylesheet
//        BStyleSheet style = null;
//        InputStreamReader istir = null;
//        try {
//
//            InputStream stin = getClass().getClassLoader().
//                    getResourceAsStream("rsrc/style2.bss");
//            istir = new InputStreamReader(stin);
//            style = new BStyleSheet(istir,
//                                    new BStyleSheet.DefaultResourceProvider());
//        } catch (Exception e) {
//            e.printStackTrace(System.err);
//            System.exit(-1);
//        } finally {
//            if (istir != null) {
//                try {
//                    istir.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }

        createWindows(_root, BuiSystem.getStyle());

        // these just get in the way
//        KeyBindingManager.getKeyBindingManager().remove("toggle_pause");
//        KeyBindingManager.getKeyBindingManager().remove("toggle_wire");
//        KeyBindingManager.getKeyBindingManager().remove("toggle_lights");
//        KeyBindingManager.getKeyBindingManager().remove("toggle_bounds");
//        KeyBindingManager.getKeyBindingManager().remove("camera_out");
        gameState.getDisplay().getRenderer().setBackgroundColor(ColorRGBA.gray);
	}
	
	protected abstract void createWindows(BRootNode root,
            BStyleSheet style);

	public void updateCore(float time) {
		uiUpdate(time);
	}
	
	public abstract void uiUpdate(float time);
}
